<?php
/*
 * 获取 passport 外域扩展信息类
 */
class Passport_PPinfo
{

	/**
	 * 判断 ppinf 的有效性
	 *
	 * @return bool
	 */
	public static function has_valid_ppinf()
	{
		$sohu_passport_ppinf = self::sohu_passport_ppinf();
		if(!isset($sohu_passport_ppinf['refuserid'])) {
			return FALSE;
		}
		return TRUE;
	}

	/**
	 * 设置 passport_info 信息，增加nickname
	 *
	 * @return array(
	 *	'userid'   => passport,
	 *	'username' => 'username',
	 *	'domain'   => 'string',
	 *	'nickname' => refname,
	 * )
	 */
	public static function build_passport_info($passport_info)
	{
		$sohu_passport_ppinf = self::sohu_passport_ppinf();
		if(isset($sohu_passport_ppinf['refnick'])) {
			$passport_info['nickname'] = urldecode($sohu_passport_ppinf['refnick']);
		}
		return $passport_info;
	}

	/**
	 * 拿到当前cookie中的ppinf值，看是否可获取到refuserid 和 refnick  可以拿到，证明是外域用户
	 * 
	 * @return 
	 * array(
	 *	'refuserid' => string(userid),
	 *	'refnick'   => string(nickname),
	 * )
	 */
	private static function sohu_passport_ppinf()
	{
		$ppinf                     = array();
		if(isset($_COOKIE['ppinf'])) {
			$sohu_passport_ppinf       = $_COOKIE['ppinf'];
			$sohu_passport_ppinf_array = explode('|',$sohu_passport_ppinf);
			if(isset($sohu_passport_ppinf_array[3])) {
				$sohu_passport_ppinf_s = $sohu_passport_ppinf_array[3];
				$ppinf_decode          = base64_decode($sohu_passport_ppinf_s);
				$ppinf_arr             = explode('|', $ppinf_decode);
				if(!empty($ppinf_arr)) {
					$inf_arr = array();
					foreach( $ppinf_arr as $p ) {
						$p_values   = explode(':', $p);
						if(isset($p_values[0]))
							$inf_arr[$p_values[0]]  = isset($p_values[2]) ? $p_values[2] : '';
					}
				}
			}
		}

		if(isset($inf_arr['refuserid']))
			$ppinf['refuserid'] = $inf_arr['refuserid'];
		if(isset($inf_arr['refnick']))
			$ppinf['refnick']   = $inf_arr['refnick'];
		
		return $ppinf;
	}

}
